﻿using System;
using System.ComponentModel;

namespace SenetExperimentsRunner.Utils.TypeDescriptors
{
    public class MyTypeDescriptionProvider : TypeDescriptionProvider
    {
        private ICustomTypeDescriptor td;
        private string[] m_PropertiesToExpand = null;
        private string m_PropertyPrefix = null;
        private bool m_ShowPropertyParent = false;

        public MyTypeDescriptionProvider()
        {
            
        }

        public MyTypeDescriptionProvider(
            Type type,
            string[] propertiesToExpand,
            string propertyPrefix,
            bool showPropertyParent)
            : this(TypeDescriptor.GetProvider(type))
        {
            m_PropertiesToExpand = propertiesToExpand;
            m_PropertyPrefix = propertyPrefix;
            m_ShowPropertyParent = showPropertyParent;
        }

        public MyTypeDescriptionProvider(TypeDescriptionProvider parent)
            : base(parent)
        {

        }

        public override ICustomTypeDescriptor GetTypeDescriptor(Type objectType, object instance)
        {
            if (td == null)
            {
                td = base.GetTypeDescriptor(objectType, instance);
                if (m_PropertiesToExpand != null &&
                    m_PropertiesToExpand.Length > 0)
                {
                    td = new MyCustomTypeDescriptor(
                        td, m_PropertiesToExpand,
                        m_PropertyPrefix,
                        m_ShowPropertyParent);
                }
                
            }
            return td;
        }
    }
}